package menu

import (
	"context"
	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"
	"fast-boot/app/model"
	"github.com/zeromicro/go-zero/core/logx"
)

type OptionsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewOptionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OptionsLogic {
	return &OptionsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *OptionsLogic) Options() (resp []*types.SysMenuOption, err error) {
	menuModel := model.SysMenuModel{}
	var items []*model.SysMenuModel
	l.svcCtx.GormClient.GormDb.Model(menuModel).
		Where("visible = true").
		Order("sort asc, id asc ").
		Scan(&items)
	return GetTreeMenu(items, 0), nil
}

func GetTreeMenu(list []*model.SysMenuModel, parentID int64) []*types.SysMenuOption {
	res := make([]*types.SysMenuOption, 0)
	for _, item := range list {
		if item.ParentID == parentID {
			var dept types.SysMenuOption
			dept.Label = item.Name
			dept.Value = item.Id
			dept.Children = GetTreeMenu(list, item.Id)
			res = append(res, &dept)
		}
	}
	return res
}
